#! /usr/bin/perl
# use Proc::Daemon;
use 5.010_000;
use strict;
use warnings;
use ODF::lpOD;
use ODF::lpOD::Table;
#Necessite en plus d'ODF::lPod : Perl 5.10, Archive::Zip 1.30 et XML::Twig 3.34
my @ls = `ls *.aadl`;
foreach (@ls)
{
    $_ =~ s/\n//;
    my $doc = odf_document->create('text');
    print " ---\n --- Opening $_ ---\n\n";
    open(FILE, $_) || die ("Conf file failed to open\n");
    my @filetab = <FILE>;

    $_ =~ s/.aadl//;
    my @allsubprograms;
    my @allthreads;
    my @tokens;

    my $idata = 0;
    my @data;
    my $subfct = 0;
    my $current = 0;
    my $curtype;
    my $tmp;
    close (FILE);
    my $s = odf_style->create('text', name => "style1");
    $doc->insert_style($s);
    $s->set_properties(
	area		=> 'text',
	size		=> '12pt',
	color		=> 'blue'
	);
   my $s2 = odf_style->create('text', name => "style2");
    $doc->insert_style($s2);
    $s2->set_properties(
	area		=> 'text',
	weight		=> 'bold',
	size		=> '14pt',
	);
   my $s3 = odf_style->create('text', name => "style3");
    $doc->insert_style($s3);
    $s3->set_properties(
	area		=> 'text',
	weight		=> 'bold',
	size		=> '18pt',
	color		=> 'red'
	);

    foreach (@filetab)
    {
	if ($_ =~ "^end")
	{
	    $subfct = 0;
	}
	if (($_ =~ "properties") || ($_ =~ "features"))
	{
	    $subfct = 1;
	    my $p = odf_paragraph->create(text => "$_");
	    my $contexte = $doc->get_body;
	    $p->set_span(style => "style1");
	    $contexte->append_element($p);
	    print "$_";
	}
	elsif ($subfct == 1)
	{
	    if ($current == 1)
	    {
		$_ =~ s/  //;
		push(@data, $_);
		my $p = odf_paragraph->create(text => "    $_");
		my $contexte = $doc->get_body;
		$contexte->append_element($p);
		print "    $_";
	    }
	    if ($current == 2)
	    {
		$_ =~ s/  //;
		push(@allsubprograms, $_);
		my $p = odf_paragraph->create(text => "    $_");
		my $contexte = $doc->get_body;
		$contexte->append_element($p);
		print "    $_";
	    }
	    if ($current == 3)
	    {
		$_ =~ s/  //;
		push(@allthreads, $_);
		my $p = odf_paragraph->create(text => "    $_");
		my $contexte = $doc->get_body;
		$contexte->append_element($p);
		print "    $_";
	    }
	}
	if ($_ =~ "^data") # number 1
	{
	    $current = 1;
	    $data[0] = substr($_, 4);
	    push(@tokens, substr($_, 4));
	    print "\n-- data --" . substr($_, 4) . "";
	    my $p = odf_paragraph->create(text => "\n-- data --" . substr($_, 4) . "");
	    $p->set_span(style => "style2");
	    my $contexte = $doc->get_body;
	    $contexte->append_element($p);
	}
	if ($_ =~ "^subprogram") # number 2
	{
	    $current = 2;
	    push(@allsubprograms, "subprogram");
	    push(@allsubprograms, substr($_, 10));
	    push(@tokens, substr($_, 10));
	    print "\n-- subprogram --" . substr($_, 10) . "";
	    my $p = odf_paragraph->create(text => "\n-- subprogram --" . substr($_, 10) . "");
	    $p->set_span(style => "style2");
	    my $contexte = $doc->get_body;
	    $contexte->append_element($p);
	}
	if (($_ =~ "^thread") && !($_ =~ "implementation")) # number 3
	{
	    $current = 3;
	    push(@allthreads, "thread");
	    push(@allthreads, substr($_, 6));
	    push(@tokens, substr($_, 6));
	    my $p = odf_paragraph->create(text => "\n-- thread --" . substr($_, 6) . "");
	    $p->set_span(style => "style2");
	    my $contexte = $doc->get_body;
	    $contexte->append_element($p);
	    print "\n-- thread --" . substr($_, 6) . "";
	}
    }
    $curtype = "";
    print "\n\n-- Dependencies --\n\n";
    my $p = odf_paragraph->create(text => "\n\n-- Dependencies --\n\n");
    $p->set_span(style => "style3");
    my $contexte = $doc->get_body;
    $contexte->append_element($p);
	  foreach (@filetab)
    {
	if ($_ =~ "^data") # number 1
	{
	    $curtype = substr($_, 4);
	}
	elsif ($_ =~ "^subprogram") # number 2
	{
	    $curtype = substr($_, 10);
	}
	elsif (($_ =~ "^thread") && !($_ =~ "implementation")) # number 3
	{
	    $curtype = substr($_, 6);
	}
	$_ =~ s/\n/;/;
	$curtype =~ s/\n/;/;
	$tmp = $_;
	foreach(@tokens)
	{
	    $_ =~ s/\n/;/;
	    if (($tmp =~ $_) && ($_ ne $curtype))
	    {
		print "$_ used in$curtype\n";
		my $p = odf_paragraph->create(text => "$_ used in$curtype\n");
		my $contexte = $doc->get_body;
		$contexte->append_element($p);

	    }
	}
    }
    $doc->save(target => "$_.odt");
}
